var totalCount=1;//总记录数
var pageCount=1;//总页数
var currentPage=1;//当前页
//通告列表用的生成页面的一些方法
$(function() {
	
   if(!window.ActiveXObject){
	   $("#selectPanel").css("margin-right","4px");
	   $("#selectPanel").css("margin-top","4px");
	   $("#selectPanel").css("padding-right","0px");
	   $("#deleteAll").css("width","69px");
	   $("#deleteAll").css("padding-left","0px");
    }
   GetCategory();
   GetList();

});
//get the konwledge category
function GetCategory(){
	QuesDwr.GetCategory(function(data){
		$("#kind").find("option").remove();
		$("#kind").append("<option value='0'>请选择</option>"); 
		$("#xzkind").find("option").remove();
		$("#tkkind").find("option").remove();
		$("#jdkind").find("option").remove();
		$("#pdkind").find("option").remove();
		for(var i=0;i<data.length;i++){
	       $("#kind").append("<option value='"+data[i].id+"'>"+data[i].name+"</option>"); 
	       $("#xzkind").append("<option value='"+data[i].id+"'>"+data[i].name+"</option>"); 
	       $("#tkkind").append("<option value='"+data[i].id+"'>"+data[i].name+"</option>"); 
	       $("#jdkind").append("<option value='"+data[i].id+"'>"+data[i].name+"</option>"); 
	       $("#pdkind").append("<option value='"+data[i].id+"'>"+data[i].name+"</option>"); 
		}
	});
}
//======出现弹出框========
function openBgKuang(obj){
	//加载遮罩
	var heights=document.body.scrollHeight;
	var bW=$("body").width();
	if(window.ActiveXObject){
	  bW=$("body").width()-22;
    }
	$("#fullbg").css({width:bW,height:heights+"px",display:"block"});
	$("#"+obj).css({display:"block"});
	//$("#kind").css("display","none");
	//$("#type").css("display","none");
}
//======出现弹出框结束========
//初始化页面数据
function GetList(){
	var cid=$("#kind").attr("value");
	var type=$("#wttype").attr("value");
	var state=$("#wtstate").attr("value");
	var problem=$("#tkey").attr("value");
	var inttype=0;
	var intstate=0;
	if(type=="-1"){
		inttype=-1;
	}else{
		inttype=Number(type);
	}
	if(state=="-1"){
		intstate=-1;
	}else{
		intstate=Number(state);
	}
   	QuesDwr.getList(currentPage,Number(cid),inttype,intstate,problem,function(data){
   		totalCount=data.totalRecords;
   		pageCount=data.totalPages;
   		currentPage=data.pageNo;
   		if(currentPage==1){
   			$("#fstPage").attr("src","img/gl/130.jpg");
   			$("#forePage").attr("src","img/gl/140.jpg");
   			$("#fstPage").css("cursor","default");
   			$("#forePage").css("cursor","default");
   		}else{
   			$("#fstPage").attr("src","img/gl/13.jpg");
   			$("#forePage").attr("src","img/gl/14.jpg");
   			$("#fstPage").css("cursor","pointer");
   			$("#forePage").css("cursor","pointer");
   		}
   		if(currentPage==pageCount){
   		  	$("#nextPage").attr("src","img/gl/150.jpg");
   			$("#nextPage").css("cursor","default");
   		}else{
   			$("#nextPage").attr("src","img/gl/15.jpg");
   			$("#nextPage").css("cursor","pointer");
   		}
	   $("#pageinfo").html("共计"+data.totalRecords+"道问题，当前第"+data.pageNo+"页，共"+data.totalPages+"页。");
	   var trs="";
	   var xuhao=(data.pageNo-1)*data.pageSize+1;
	   for(var i=0;i<data.questions.length;i++){
		   trs=trs+trtemplate.replace("#Title#",data.questions[i].problem)
		                     .replace("#Title#",getByteVal(data.questions[i].problem,80))
		                     .replace("#Cid#",data.questions[i].name)
		                     .replace("#typeid#",data.questions[i].type)
		                     .replace("#typeid#",data.questions[i].type)
		                     .replace("#typeid#",data.questions[i].type)
		                     .replace("#Type#",data.questions[i].typestr)
		                     .replace("#IDType#",data.questions[i].id+"#"+data.questions[i].type)
		                     .replace("#ID#",data.questions[i].id)
		                     .replace("#ID#",data.questions[i].id)
		                     .replace("#State#",data.questions[i].statestr)
		                     .replace("#ID#",data.questions[i].id)
		                     .replace("#Level#",data.questions[i].levelstr)
		                     .replace("#RowNum#",xuhao++);
	   }
	   $("#ttbody").html(trs);
	   cid=null;
	   type=null;
	   problem=null;
	   inttype=null;
	   level=null;
	   intstate=null;
   });
}
//搜索
function Search(){
	currentPage=1;//当前页
	GetList();
}
//重置
function Reset(){
	$("#kind").attr("value","0");
	$("#wttype").attr("value","");
	$("#tkey").attr("value","");
}
//跳转页面
function PageGo()
{
	 var page=$("#gopage").val();
	  var regNum =/^\d*$/;
	  if(page.length>0){
		  if(regNum.test(page)){
			  currentPage=parseInt(page)>pageCount?pageCount:parseInt(page);
			  if(currentPage>0){
				  GetList();
			  }else{
				  alert("请输入大于零的数字！");
				  $("#gopage").attr("value","");
				  $("#gopage").focus();
			  }
		  }else{
			  alert("只能输入数字");
			  $("#gopage").attr("value","");
			  $("#gopage").focus();
			  }
		}else{
          alert("请输入您要跳转的页数");
		  $("#gopage").focus();
			}
	  regNum=null;
	  page=null;
}
//翻页
function ChangePage(cmd){
	if("fst"==cmd){
		//首页
		if(currentPage!=1){
			currentPage=1;
			GetList();
		}
	}else if("fore"==cmd){
		//上一页
		if(currentPage!=1){
			currentPage--;
			GetList();
		}
	}else if("next"==cmd){
		//下一页
		if(currentPage!=pageCount){
			currentPage++;
			GetList();
		}
	}
}
//close the show window
function closeShow(){
	$("#fullbg").css({display:"none"});
	$("#showpanel").css({display:"none"});
}
//审核问题
function Check(id,state){
	if(state==1||state==2){
		QuesDwr.CheckQues(id,state,function(data){
			if(data==0){
				alert("审核失败！");
			}else{
				GetList();
			}
		});
	}
}
//浏览审核通过
function CheckTG(){
	var id=$("#tid").val();
	Check(Number(id),1);
	closeShow();
}
//浏览审核不通过
function CheckBTG(){
	var id=$("#tid").val();
	Check(Number(id),2);
	closeShow();
}
//view the notice by id
function View(id,type){
	$("#tid").val(id);
	QuesDwr.GetQuesById(id,function(data){
		if(data!=null){
			if(type==0){
				$("#xxkindshow").html(data.typestr+"题");
				$("#problemshow").val(data.problem);
				$("#xxashow").val(data.op1);
				$("#xxbshow").val(data.op2);
				$("#xxcshow").val(data.op3);
				$("#xxdshow").val(data.op4);
				if(data.op1.length>30){
					$("#xxashow").removeClass("readonly").addClass("autoheight")
				}else{
					$("#xxashow").removeClass("autoheight").addClass("readonly")
				}
				if(data.op2.length>30){
					$("#xxbshow").removeClass("readonly").addClass("autoheight")
				}else{
					$("#xxbshow").removeClass("autoheight").addClass("readonly")
				}
				if(data.op3.length>30){
					$("#xxcshow").removeClass("readonly").addClass("autoheight")
				}else{
					$("#xxcshow").removeClass("autoheight").addClass("readonly")
				}
				if(data.op4.length>30){
					$("#xxdshow").removeClass("readonly").addClass("autoheight")
				}else{
					$("#xxdshow").removeClass("autoheight").addClass("readonly")
				}
				$("#xxlevelshow").html(data.levelstr);
				var ans="";
				var arrmp =data.ans.split("|");
				for(var i=0;i<arrmp.length;i++){
					if(check_Num(arrmp[i])){
						if(arrmp[i]==1){
							ans+="A ";
						}else if(arrmp[i]==2){
							ans+="B ";
						}else if(arrmp[i]==3){
							ans+="C ";
						}else if(arrmp[i]==4){
							ans+="D ";
						}
					}
				}
				$("#xxansshow").html(ans);
				$("#xxshow").css("display","");
				$("#tkshow,#jdshow,#pdshow").css("display","none");
			}else if(type==1){
				$("#tkkindshow").html(data.typestr+"题");
				$("#tkproblemshow").html(data.problem);
				$("#tkansshow").html(data.ans);
				if(data.ans.length>30){
					$("#tkansshow").removeClass("readonly").addClass("autoheight")
				}else{
					$("#tkansshow").removeClass("autoheight").addClass("readonly")
				}
				$("#tklevelshow").html(data.levelstr);
				$("#tkshow").css("display","");
				$("#xxshow,#jdshow,#pdshow").css("display","none");
			}else if(type==2){
				$("#pdkindshow").html(data.typestr+"题");
				$("#pdproblemshow").html(data.problem);
				$("#pdansshow").html(data.ans=="0"?"错误":"正确");
				$("#pdlevelshow").html(data.levelstr);
				$("#pdshow").css("display","");
				$("#xxshow,#jdshow,#tkshow").css("display","none");
			}else if(type==3){
				$("#jdkindshow").html(data.typestr+"题");
				$("#jdproblemshow").html(data.problem);
				$("#jdansshow").html(data.ans);
				$("#jdlevelshow").html(data.levelstr);
				$("#jdshow").css("display","");
				$("#xxshow,#pdshow,#tkshow").css("display","none");
			}
			openBgKuang("showpanel");
		}else{
			alert("该问题已被删除！");
		}
	});
}

//tr模板
var trtemplate='<tr>'+
				'					<td style="width: 40px;">'+
				'						#RowNum#'+
				'					</td>'+
				'					<td style="width: 80px;">'+
				'						#Cid#'+
				'					</td>'+
				'					<td style="width: 60px;">'+
				'						#Type#'+
				'					</td>'+
				'					<td style="width: 60px;">'+
				'						#Level#'+
				'					</td>'+
				'					<td style="text-align: left;padding-left:2px;" title="#Title#">'+
				'						<span style="cursor:pointer;" onclick="View(#ID#,#typeid#)">#Title#</span>'+
				'					</td>'+
				'					<td style="width: 60px;">'+
				'						#State#'+
				'					</td>'+
				'					<td style="width: 120px;">'+
				'						<table class="intable">'+
				'							<tr>'+
				'								<td style="border: 0px;">'+
				'									<span class="check button" onclick="Check(#ID#,1)">通过</span>'+
				'								</td>'+
				'								<td style="border: 0px;">'+
				'									<span class="check button" style="margin-left:3px;" onclick="Check(#ID#,2)">不通过</span>'+
				'								</td>'+
				'							</tr>'+
				'						</table>'+
				'					</td>'+
				'				</tr>';
